package mysql

import (
	"gorm.io/gorm"
	"practice-one/userrpc/userrpc"
)

type Collect struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null;comment:用户id"`
	GoodsId int64 `gorm:"type:int(11);not null;comment:商品id"`
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) CollectFind(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("user_id =? and goods_id =?", id, id2).Find(c).Error
}

func (c *Collect) DelCollect(db *gorm.DB, id uint) error {
	return db.Debug().Where("id =?", id).Delete(c).Error
}

/*
SELECT
  goods.id goods_id,
  goods.content,
  goods.goods_type,
  goods.price,
  goods.title
FROM
  collects
  LEFT JOIN goods ON collects.goods_id = goods.id
*/

func CollectList(db *gorm.DB, id int64) []*userrpc.CollectList {
	var list []*userrpc.CollectList
	db.Debug().Model(&Collect{}).
		Select(
			"goods.id goods_id",
			"goods.content",
			"goods.goods_type",
			"goods.price",
			"goods.title",
		).
		Joins("LEFT JOIN goods ON collects.goods_id = goods.id").
		Where("collects.user_id =?", id).
		Find(&list)
	return list

}
